<?php


namespace app\api\controller;


use think\Db;

class Host extends Lib
{

    // 自定义
    public function zbx_host_custom_get()
    {
        $params = [
            "output" => ["hostid", "host", "name"],
            "search" => ["name" => ["华为", "防火墙", "路由", "交换"]],
            "excludeSearch" => true
        ];

        return $this->zbx_host_get($params);

    }

    // 系统
    public function zbx_host_system_get()
    {
        $params = [
            "output" => ["hostid", "host", "name"],
            "search" => ["name" => ["华为", "防火墙", "路由", "交换", "智能家居事业部", "大视频事业部", "家庭网络事业部", "Zabbixserver"]],
            "excludeSearch" => true
        ];

        return $this->zbx_host_get($params);
    }

    // 获取host
    public function zbx_host_get($params)
    {
        $result = json_decode(self::RPC('host.get', $params), true);
        $data = $result['data'];
        $dept = Db::name('dept')->column('id,name');

        if ($data){
            $add_data = [];
            foreach ($data as $k=>&$v){
                $host_name = explode('-', $v['name']);

                foreach ($host_name as $kk=>$vv){
                    if (in_array($vv,$dept)){
                        $dept_id = array_search($vv, $dept);
                        $is_host = Db::name('hosts')->where('hostid', $v['hostid'])->find();
                        if (!$is_host){
                            $add_data[$k]['hostid'] = $v['hostid'];
                            $add_data[$k]['name'] = $v['name'];
                            $add_data[$k]['deptid'] = $dept_id;
                        }
                        break;
                    }
                }
            }

            if ($add_data){
                $add = Db::name('hosts')->insertAll($add_data);
                if ($add){
                    return true;
                }else{
                    return false;
                }
            }else{
                return true;
            }
        }else{
            return true;
        }
    }
}